#include "ys_senior_check.h"
#include "ui_ys_senior_check.h"

#include "pub_var.h"
#include <QSettings>
#include <QDebug>
#include <QMessageBox>

Ys_senior_check::Ys_senior_check(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Ys_senior_check)
{
    ui->setupUi(this);

    //恢复当前窗口位置
    QSettings setIni("clin.ini", QSettings::IniFormat);
    QRect rct = setIni.value("windowState/senior_check").toRect();
    this->setGeometry(rct);

    //数据库连接
    qrm = new QSqlQuery(pub::db_m);
    qro = new QSqlQuery(pub::db_o);
    qrot = new QSqlQuery(pub::db_o);
    md = new QSqlQueryModel();

    tm_window_open = new QTimer(this);
    connect( tm_window_open, &QTimer::timeout, this, &Ys_senior_check::wind_open_time_out);
    tm_window_open->start(300);

}

void Ys_senior_check::wind_open_time_out()
{
    tm_window_open->stop();
    on_pb_search_clicked();
}

void Ys_senior_check::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setValue("windowState/senior_check", QVariant(this->geometry()));

    emit destroyW( "senior_check" );

}

Ys_senior_check::~Ys_senior_check()
{
    delete ui;
}

void Ys_senior_check::on_pb_exit_clicked()
{
    close();
}

void Ys_senior_check::on_pb_search_clicked()
{
    sq = "select distinct apply_doct_name 医师姓名,card_no 病志号, patient_name 患者姓名," //0-2
         " recipe_no, clinic_code, item_code " //3-5
         " from pha_sto_recipe_senior_check"
         " where "
         //" apply_date > trunc(sysdate) - 2 and "
         " dept_code = '%1'"//
         " and check_status = '0'"
         " and '%2' <= grade_need";//pub::dr_profttl
    sq = sq.arg(pub::dp_code).arg(pub::dr_profttl);
    qDebug()<<"提取可以审批的下级医师处方："<<sq<<"\n";
    //qro->exec(sq);
    md->setQuery(sq, pub::db_o);
    int w = ui->tv_patient_list->width();
    ui->tv_patient_list->setModel(md);
    pub::setColumnWidth(ui->tv_patient_list, "0.3^0.3^0.3^0^0^0", w);

    ui->pt_patient_detail->clear();
}

void Ys_senior_check::on_tv_patient_list_doubleClicked(const QModelIndex &index)
{
    on_tv_patient_list_clicked(index);
}

void Ys_senior_check::on_tv_patient_list_clicked(const QModelIndex &index)
{

    ui->pt_patient_detail->clear();
    QString clinic_code = index.siblingAtColumn(4).data().toString();

    sq = "select xmmc,xmjg from ys_bz_e_lib where clinic_code = '" + clinic_code + "'";
    qrm->exec(sq);
    QString str_lib;
    if(qrm->next())
    {
        str_lib = "检查化验：" + qrm->value(0).toString() +"/"+ qrm->value(1).toString() + "\n";
    }

    sq = "select * from ys_bz_e_rec r where clinic_code = '" + clinic_code + "'";

    QString t;
    qrm->exec(sq);

    if(qrm->next())
    {
        t.append("患者主诉：" + qrm->value("hzzs").toString() + "\n\n");   //主诉;
        t.append("现病史:" + qrm->value("xbs").toString() + "\n\n");     //现病史;
        t.append("既往史：" + qrm->value("jws").toString() + "\n\n");     //既往史
        //t.append("过敏史：" + qrm->value("gms").toString() + "\n\n");     //过敏史;
        //t.append("个人史：" + qrm->value("person_hist").toString() + "\n\n");     //个人史;
        //t.append("家族史：" + qrm->value("family_hist").toString() + "\n\n");     //家族史;
        t.append("体格检查：" + qrm->value("tgjc").toString() + "\n\n");   //体格检查;

//        bool is_tr = false;
//        if(!qrm->value("zyzd1").toString().isEmpty())     //中医诊断
//        {
//            is_tr = true;
//        }

//        if(is_tr)
//        {
//            t.append("中医查体:" + qrm->value("tgjc_tr").toString() + "\n\n");   //中医查体
//            t.append("中医辩证:" + qrm->value("zybz").toString() + "\n\n");   //中医辩证
//            t.append("中医类证鉴别:" + qrm->value("similar_syndrom").toString() + "\n\n");   //中医辩证
//            t.append("中医治则:" + qrm->value("treat_tr").toString() + "\n\n");   //中医辩证
//        }

        if(!str_lib.isEmpty())
        {
            t.append(str_lib);
        }

        QString zyzd,xyzd;  //中医诊断，西医诊断

//        if(!qrm->value("zyzd1").toString().isEmpty())
//        {
//            zyzd = qrm->value("zyzd1").toString() + "." + qrm->value("zyzh1").toString() ;
//        }

//        if(!qrm->value("zyzd2").toString().isEmpty())
//        {
//            zyzd += " / " + qrm->value("zyzd2").toString() + "." + qrm->value("zyzh2").toString() ;
//        }

//        if(!qrm->value("zyzd3").toString().isEmpty())
//        {
//            zyzd += " / " + qrm->value("zyzd3").toString() + "." + qrm->value("zyzh3").toString() ;
//        }

        xyzd = qrm->value("zd1").toString();    //西医诊断至少有一个

        if(!qrm->value("zd2").toString().isEmpty()){
            xyzd.append(" / " + qrm->value("zd2").toString());
        }

        if(!qrm->value("zd3").toString().isEmpty()){
            xyzd.append(" / " + qrm->value("zd3").toString());
        }

        t.append("西医诊断：" + xyzd + "\n\n");
//        if(is_tr)
//        {
//            t.append("中医诊断：" + zyzd + "\n\n");
//        }

        if(!qrm->value("medical_plan").toString().isEmpty())
        {
            t.append("诊疗计划:" + qrm->value("medical_plan").toString() + "\n\n");         //医嘱
        }
    }

    ui->pt_patient_detail->appendPlainText( t);

}

void Ys_senior_check::on_pb_save_clicked()
{

    if(!ui->tv_patient_list->currentIndex().isValid())
    {
        QMessageBox::information(this, "注意", "未选择有效患者信息 ！");
        return;
    }

    QString clinic_code = ui->tv_patient_list->currentIndex().
            siblingAtColumn(4).data().toString();
    QString item_code = ui->tv_patient_list->currentIndex().
            siblingAtColumn(5).data().toString();

    QString check_note = ui->te_check_note->toPlainText().trimmed();
    QString check_result = "";
    QString recipe_no = "";

    if(ui->cbb_result->currentText() != "同意")
    {
        check_result = "2";

        if(check_note.isEmpty())
        {
            QMessageBox::information(this, "注意", "不同意下级医师用药方案，审核意见不可以为空 ！");
            return;
        }
    }
    else
    {
        check_result = "1";
    }

    sq = " update pha_sto_recipe_senior_check set "
         //" recipe_no = '%1',"
         " check_doct_code = '%2',"
         " check_doct_name = '%3',"
         " check_date = sysdate,"
         " check_status = '%4',"
         " check_note = '%5'"
         " where clinic_code = '%6'"
         " and check_status = '0'"
         " and grade_need >='%7'";
    sq = sq //.arg(reci_lst.join("^"))
            .arg(pub::usr_code)
            .arg(pub::usr_name)
            .arg(check_result)
            .arg(check_note)
            .arg(clinic_code)
            .arg(pub::dr_profttl);
    qDebug()<<"保存审批信息："<<sq<<"\n";

    qro->exec(sq);

    QMessageBox::information(this, "注意", "审批信息已经保存！");

    sq = " delete from com_events where clinic_code = '%1'";

    sq = sq.arg(clinic_code);
    qDebug()<<"撤销通知信息："<<sq<<"\n";

    qro->exec(sq);

    on_pb_search_clicked();
}
